//
//  Data.m
//  dingding
//
//  Created by dingding on 15/12/2.
//  Copyright © 2015年 dingding. All rights reserved.
//

#import "ViewUtils.h"
#import "UIView+Activity.h"
#include "Toast.h"
#import <MediaPlayer/MediaPlayer.h>
#import "XLAlertView.h"

#define Tag_nodatas   752

@implementation ViewUtils

+(void)showToast:(NSString*)content;
{
    [[Toast makeText:content] show];
}

+(void)labelSetSpecailTxt:(UILabel*)label str:(NSString*)all start:(NSString*)txt1 end:(NSString*)txt2 color:(UIColor*)color
{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:all];
    unsigned long startindex=txt1?[all rangeOfString:txt1].location:0;
    unsigned long endindex=txt2?[all rangeOfString:txt2].location:all.length;
    NSRange range = NSMakeRange(startindex+1, endindex-startindex-1);
    [noteStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    [label setAttributedText:noteStr];
    [label sizeToFit];
}

+(void)labelSetUnderline:(UILabel*)label str:(NSString*)all start:(NSString*)txt1 end:(NSString*)txt2
{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:all];
    unsigned long startindex=txt1?[all rangeOfString:txt1].location:0;
    unsigned long endindex=txt2?[all rangeOfString:txt2].location:all.length;
    NSRange range = NSMakeRange(startindex+1, endindex-startindex-1);
    [noteStr addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
    [label setAttributedText:noteStr];
    [label sizeToFit];
}

+(void)labelSetUnderline1:(UILabel*)label str:(NSString*)all start:(NSString*)txt1 end:(NSString*)txt2 color:(UIColor*)color
{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:all];
    unsigned long startindex=txt1?[all rangeOfString:txt1].location:0;
    unsigned long endindex=txt2?[all rangeOfString:txt2].location:all.length;
    NSRange range = NSMakeRange(startindex+1, endindex-startindex-1);
    [noteStr addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
    [noteStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    [label setAttributedText:noteStr];
    [label sizeToFit];
}

+(void)show1Alert:(NSString*)title content:(NSString*)content ok:(NSString*)ok
{
    if(!ok){
        ok=@"确定";
    }
//    [[[UIAlertView alloc] initWithTitle:title
//                                message:content
//                       cancelButtonItem:[RIButtonItem itemWithLabel:ok action:^{
//        // Handle "Cancel"
//    }]
//                       otherButtonItems:nil , nil] show];
    
    XLAlertView *xlAlertView = [[XLAlertView alloc] initWithTitle:title message:content sureBtn:nil cancleBtn:ok];
    xlAlertView.resultIndex = ^(NSInteger index){
        //回调---处理一系列动作
    };
    [xlAlertView showXLAlertView];
}

+(void)show1Alert:(NSString*)title content:(NSString*)content ok:(NSString*)ok action:(void (^)(void))func
{
    if(!ok){
        ok=@"确定";
    }
//    [[[UIAlertView alloc] initWithTitle:title
//                                message:content
//                       cancelButtonItem:[RIButtonItem itemWithLabel:ok action:func]
//                       otherButtonItems:nil , nil] show];
    XLAlertView *xlAlertView = [[XLAlertView alloc] initWithTitle:title message:content sureBtn:ok cancleBtn:nil];
    xlAlertView.resultIndex = ^(NSInteger index){
        //回调---处理一系列动作
        if(func)func();
    };
    [xlAlertView showXLAlertView];
}

+(void)show2Alert:(NSString*)title content:(NSString*)content ok:(NSString*)ok action:(void (^)(void))func
{
    if(!ok){
        ok=@"确定";
    }
//    [[[UIAlertView alloc] initWithTitle:title
//                                message:content
//                       cancelButtonItem:[RIButtonItem itemWithLabel:@"取消" action:^{
//        // Handle "Cancel"
//    }]
//                       otherButtonItems:[RIButtonItem itemWithLabel:ok action:func],nil] show];
    XLAlertView *xlAlertView = [[XLAlertView alloc] initWithTitle:title message:content sureBtn:ok cancleBtn:@"取消"];
    xlAlertView.resultIndex = ^(NSInteger index){
        //回调---处理一系列动作
        if(func)func();
    };
    [xlAlertView showXLAlertView];
}

+(void)show2Alert:(NSString*)title content:(NSString*)content ok:(NSString*)ok cancel:(NSString*)cancel action:(void (^)(void))func
{
    if(!ok){
        ok=@"确定";
    }
    if(!cancel){
        cancel=@"取消";
    }
//    [[[UIAlertView alloc] initWithTitle:title
//                                message:content
//                       cancelButtonItem:[RIButtonItem itemWithLabel:cancel action:^{
//        // Handle "Cancel"
//    }]
//                       otherButtonItems:[RIButtonItem itemWithLabel:ok action:func],nil] show];
    XLAlertView *xlAlertView = [[XLAlertView alloc] initWithTitle:title message:content sureBtn:ok cancleBtn:cancel];
    xlAlertView.resultIndex = ^(NSInteger index){
        //回调---处理一系列动作
        if(func)func();
    };
    [xlAlertView showXLAlertView];
}

+(UIView *)findFirstResponder:(UIView*)root
{
    if (root.isFirstResponder) {
        return root;
    }
    for (UIView *subView in root.subviews) {
        UIView *firstResponder = [ViewUtils findFirstResponder:subView];
        if (firstResponder != nil) {
            return firstResponder;
        }
    }
    return nil;
}

+(void)showOneBtnImageName:(NSString *)oneImageName OneBtnTitle:(NSString *)oneTitle TwoBtnImageName:(NSString *)twoImageName TwoBtnTitle:(NSString *)twoTitle action:(void (^)(NSInteger index))func {
    
    XLAlertView *xlAlertView = [[XLAlertView alloc] initOneBtnImageName:oneImageName OneBtnTitle:oneTitle TwoBtnImageName:twoImageName TwoBtnTitle:twoTitle];
    xlAlertView.resultIndex = ^(NSInteger index) {
        //回调---处理一系列动作
        if(func)func(index);
    };
    [xlAlertView showXLAlertView];
}

@end
